package org.lhh.server.core.repository;

import org.apache.commons.pool2.BasePooledObjectFactory;
import org.apache.commons.pool2.PooledObject;
import org.apache.commons.pool2.impl.DefaultPooledObject;
import org.pentaho.di.core.Const;
import org.pentaho.di.core.KettleEnvironment;
import org.pentaho.di.core.database.Database;
import org.pentaho.di.core.database.DatabaseMeta;
import org.pentaho.di.core.encryption.Encr;
import org.pentaho.di.core.encryption.TwoWayPasswordEncoderPluginType;
import org.pentaho.di.core.plugins.PluginRegistry;
import org.pentaho.di.core.util.EnvUtil;
import org.pentaho.di.repository.kdr.KettleDatabaseRepositoryMeta;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;

public class EtlPooledObjectFactory extends BasePooledObjectFactory<EtlKettleDatabaseRepository> {

    private static final Logger logger = LoggerFactory.getLogger(EtlPooledObjectFactory.class);

    private static final String ID = "server";

    @Override
    public boolean validateObject(PooledObject<EtlKettleDatabaseRepository> repositoryPooledObject) {
        EtlKettleDatabaseRepository repository = repositoryPooledObject.getObject();
        Database database = repository.getDatabase();
        Connection connection = database.getConnection();
        Statement statement = null;
        try {
            statement = connection.createStatement();
            statement.execute("select  1");
        } catch (SQLException e) {
            logger.error("校验资源库连接池异常丢弃", e);
            return false;
        } finally {
            if (statement != null) {
                try {
                    statement.close();
                } catch (SQLException e) {
                    //ignored
                }
            }
        }
        return true;
    }

    @Override
    public EtlKettleDatabaseRepository create() throws Exception {
        return repository();
    }

    @Override
    public PooledObject<EtlKettleDatabaseRepository> wrap(EtlKettleDatabaseRepository obj) {
        return new DefaultPooledObject<>(obj);
    }

    private EtlKettleDatabaseRepository repository() throws Exception {
        if (!KettleEnvironment.isInitialized()) {
            KettleEnvironment.init(false);
            PluginRegistry.addPluginType(TwoWayPasswordEncoderPluginType.getInstance());
            String passwordEncoderPluginID =
                    Const.NVL(EnvUtil.getSystemProperty(Const.KETTLE_PASSWORD_ENCODER_PLUGIN), "Kettle");
            Encr.init(passwordEncoderPluginID);
            PluginRegistry.init();
        }
        DatabaseMeta databaseMeta = EtlRepository.databaseMetaXml();
        KettleDatabaseRepositoryMeta kettleDatabaseMeta = new KettleDatabaseRepositoryMeta(ID, ID, null, databaseMeta);
        EtlKettleDatabaseRepository databaseRepository = new EtlKettleDatabaseRepository();
        databaseRepository.init(kettleDatabaseMeta);
        databaseRepository.connect("admin", "admin");
        return databaseRepository;
    }

}
